﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Controls.SalesManSVC;
using SMS.Common.Silverlight;
using SMS.Entities.BaseClasses;
using System.ComponentModel;

namespace SMS.Controls
{
    public partial class SMSSalesMan : UserControl
    {
        private ServiceProxy<SalesManServiceClient> proxy = null;
        public SMSSalesMan()
        {
            InitializeComponent();
            if (!DesignerProperties.IsInDesignTool)
            {
                proxy = new ServiceProxy<SalesManServiceClient>("Service/SalesManService.svc");
                proxy.Client.SearchSalesManCompleted += new EventHandler<SearchSalesManCompletedEventArgs>(Client_SearchSalesManCompleted);
                smsAutoComplete.SelectionChanged += new Action<object, object>(smsAutoComplete_SelectionChanged);
                smsAutoComplete.SearchTextChanged += new Action<string>(smsAutoComplete_SearchTextChanged);
                smsAutoComplete.TextChanged += new Action<string>(smsAutoComplete_TextChanged);
                smsAutoComplete.SetFilter(SearchSalesMan);
                smsAutoComplete.SetDataTemplate(this.Resources["SalesManTemplate"] as DataTemplate);
                smsAutoComplete.SetValuePath("SalesMan_Name");
            }
        }

        void smsAutoComplete_TextChanged(string obj)
        {
            SearchText = obj;
        }

        void smsAutoComplete_SearchTextChanged(string obj)
        {
            //throw new NotImplementedException();
            if (IsNewSearchTextRequired(obj))
            {
                currentSearchText = smsAutoComplete.autoCompleteBox1.Text;
                VisualStateManager.GoToState(smsAutoComplete, "BusyState", true);
                proxy.Client.SearchSalesManAsync(string.Empty, obj);
            }
        }

        void smsAutoComplete_SelectionChanged(object arg1, object arg2)
        {
            //throw new NotImplementedException();
            SelectedSalesMan  = arg2 as SMS.Entities.SalesManDetail;
        }

        void Client_SearchSalesManCompleted(object sender, SearchSalesManCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            smsAutoComplete.ItemSource = e.Result as IEnumerable<BaseEntity>;
            VisualStateManager.GoToState(smsAutoComplete, "Normal", true);
        }
        private bool IsNewSearchTextRequired(string searchText)
        {
            return currentSearchText.ToLower() != searchText.ToLower();
        }
        private string currentSearchText = string.Empty;
        private bool IsRunning
        {
            get { return System.Windows.Browser.HtmlPage.IsEnabled; }
        }

        public bool SearchSalesMan(string search, object value)
        {
            return (value as SMS.Entities.SalesManDetail).SalesMan_ID.ToString().Contains(search.ToLower()) ||
                (value as SMS.Entities.SalesManDetail).SalesMan_Name.ToLower().Contains(search.ToLower());
            //return true;
        }
        public Entities.SalesManDetail SelectedSalesMan
        {
            get { return (Entities.SalesManDetail)GetValue(SelectedSalesManProperty); }
            set { SetValue(SelectedSalesManProperty, value); smsAutoComplete.SetSelectedItem(value); }
        }

        // Using a DependencyProperty as the backing store for SelectedProduct.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedSalesManProperty =
            DependencyProperty.Register("SelectedSalesMan", typeof(Entities.SalesManDetail), typeof(SMSSalesMan), new PropertyMetadata(null, new PropertyChangedCallback(SelectedSalesManChanged)));

        public static void SelectedSalesManChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSSalesMan;
            control.SelectedSalesMan = e.NewValue as SMS.Entities.SalesManDetail;
            
        }



        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SearchText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SMSSalesMan), new PropertyMetadata(string.Empty,new PropertyChangedCallback(SearchTextPropertyChanged)));

         private static void SearchTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSSalesMan;
            control.SearchText = e.NewValue as string;
        }

    }
}
